package main.普通数组;

/**
 * @author tmh
 * @date 2024/7/18 16:46
 * @description
 */
public class T189轮转数组 {
    public void rotate(int[] nums, int k) {
        //思路：1.反转整个数组 2.反转前k个元素 3.反转后面len-k个元素
        int len = nums.length;
        k = k % len;
        reverse(nums, 0, len - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, len - 1);

    }

    //反转数组模板
    private void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[end];
            nums[end] = nums[start];
            nums[start] = temp;
            start++;
            end--;
        }
    }
}
